package com.lg;

/**
 * Java三个线程依次序打印1-100
 * 利用synchronized关键字
 * 
 * @author kaifa002
 *
 */
public class Main1 {
	public static void main(String[] args) {
		Task task = new Task();

		Thread thread1 = new Thread(() -> task.print(0), "thread 1");
		Thread thread2 = new Thread(() -> task.print(1), "thread 2");
		Thread thread3 = new Thread(() -> task.print(2), "thread 3");
		
		thread1.start();
		thread2.start();
		thread3.start();
	}
}

class Task1 {
	private int count = 1;

	public synchronized void print(int id) {
		while (true) {
			try {
				while (count % 3 != id) {
					wait();
				}
				if (count > 100) {					
					break;
				}
				System.out.println(Thread.currentThread().getName() + ": " + count);
				count++;
				notifyAll();
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
		}
	}
}


